package com.ibeetl.admin.core.service;

import com.ibeetl.admin.core.dao.MessageDao;
import com.ibeetl.admin.core.entity.CoreRole;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.entity.Message;
import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.admin.core.web.query.MessageQuery;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Message Service
 */

@Service
@Transactional
public class MessageService extends BaseService<Message> {

    @Autowired
    private MessageDao messageDao;

    public PageQuery<Message> queryByCondition(PageQuery query) {
        PageQuery ret = messageDao.queryByCondition(query);
        queryListAfter(ret.getList());
        return ret;
    }

    public void batchDelMessage(List<Long> ids) {
        try {
            messageDao.batchDelMessageByIds(ids);
        } catch (Exception e) {
            throw new PlatformException("批量删除Message失败", e);
        }
    }

    public List<Message> getMessageList(MessageQuery messageQuery) {
        return messageDao.getMessageList(messageQuery);
    }

    public void updateStatus(List<Message> result,Long roleId) {
        List<Integer> ids = new ArrayList<>();
        for (Message message : result) {
            if (roleId == CoreRole.PERSON && message.getStatus() == MessageQuery.NOT_READ_STATUS){
                ids.add(message.getId());
            }else if(roleId == CoreRole.COMPANY && message.getCompanyStatus() == null){
                ids.add(message.getId());
            }

        }
        if (ids.size() > 0) {
            if (roleId == CoreRole.PERSON){
                messageDao.batchUpdatePersonMessageByIds(ids);
            }else if(roleId == CoreRole.COMPANY){
                messageDao.batchUpdateCompanyMessageByIds(ids);
            }
        }
    }

    public int countNotReadMessage(CoreUser coreUser, CoreRole role) {
        MessageQuery messageQuery = new MessageQuery();
        if (role.getId() == CoreUser.COMPANY) {
            messageQuery.setCompanyId(coreUser.getId());
            return messageDao.countNotReadCompanyMessage(messageQuery);
        } else if (role.getId() == CoreUser.PERSON) {
            messageQuery.setUserId(coreUser.getId());
            return messageDao.countNotReadPersonMessage(messageQuery);
        } else {
//            超级管理员
            return 0;
        }
    }

    public List<Message> messageList(CoreUser user, CoreRole role) {
        MessageQuery messageQuery = new MessageQuery();
        List<Message> result = new ArrayList<>();
        /*if (role.getId() == CoreRole.PERSON) {
            messageQuery.setUserId(user.getId());
        }else*/
        if (role.getId() == CoreRole.COMPANY) {
            messageQuery.setCompanyId(user.getId());
        }
        if(messageQuery.getCompanyId() != null || messageQuery.getUserId() != null){
            result = getMessageList(messageQuery);
        }
        updateStatus(result,role.getId());
        return result;
    }
}